package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author liangzai
 * @Description:
 */
public class Demo1 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(1000);

        Thread producer = new Thread(()->{
            int n = 0;
            while (true) {
                try {
                    queue.put(n);
                    System.out.println("生产者生产了："+n);
                    n++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"producer");
        Thread consumer = new Thread(()->{
            int n = 0;
            while (true) {
                try {
                    n = queue.take();
                    System.out.println("消费者消费了："+n);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"consumer");
        producer.start();
        consumer.start();

    }
}














